<?php

namespace App\Admin\Controllers;

use App\Models\Coupon;
use App\Models\Store;
use App\Models\StoreDetail;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Route;

class CouponController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '优惠券';
    protected $type = [1 => '满减'];
    protected $status = [1 => '正常', 2 => '下架'];

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Coupon());

        $grid->column('name', __('Name'));
        $grid->column('type', '优惠券类型')->using($this->type);
        $grid->column('store.name', '门店');
        $grid->column('price', '优惠价格')->display(function ($data) {
            return '￥'.$data;
        })->sortable();
        $grid->column('full', '满')->display(function ($data) {
            return '￥'.$data;
        })->sortable();
        $grid->column('limit', '限领')->sortable();
        $grid->column('stock', '总库存')->sortable();
        $grid->column('sell', '已领取')->sortable();
        $grid->column('sort', __('Sort'))->sortable();
        
        $grid->column('status', __('Status'))->display(function ($status, $column) {
            if ($status == 3) {
            	return $column->switch([
		            'on' => ['text' => '正常', 'value' => 1, 'color' => 'success'],
			        'off' => ['text' => '申请', 'value' => 3, 'color' => 'danger']
		        ]);
            } else {
            	return $column->switch([
		            'on' => ['text' => '正常', 'value' => 1, 'color' => 'success'],
			        'off' => ['text' => '下架', 'value' => 2, 'color' => 'danger']
		        ]);
            }
        });
        
        $grid->column('start_time', __('Start time'));
        $grid->column('end_time', '结束时间');
		
        $grid->filter(function ($filter) {
             $filter->disableIdFilter();
             $filter->like('name', __('Name'));
             $filter->equal('store_id', __('Store id'))->select(StoreDetail::setSelect());
             $filter->equal('type', __('Type'))->radio(Arr::add($this->type, '', '全部'));
             $filter->equal('status', __('Status'))->radio(Arr::add($this->status, '', '全部'));
        });
        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Coupon::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('name', __('Name'));
        $show->field('type', __('Type'));
        $show->field('price', __('Price'));
        $show->field('full', __('Full'));
        $show->field('limit', __('Limit'));
        $show->field('stock', __('Stock'));
        $show->field('sell', __('Sell'));
        $show->field('sort', __('Sort'));
        $show->field('describe', __('Describe'));
        $show->field('status', __('Status'));
        $show->field('start_time', __('Start time'));
        $show->field('end_time', __('End time'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));
        $show->field('deleted_at', __('Deleted at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Coupon());

        $form->text('name', __('Name'))->rules('required');
        $form->radio('type', __('Type'))->options($this->type)->rules('required')->default(1);
        $form->radio('is_member', '会员专属')->options([1 => '是', 2 => '否'])->default(2);
        $form->currency('price', '优惠价格')->symbol('￥')->rules('required|numeric|min:0');
        $form->currency('full', '满')->symbol('￥')->rules('required|numeric|min:0');
        $form->number('limit', '限领')->default(1)->rules('required|numeric|min:0');
        $form->number('stock', __('Stock'))->default(0)->rules('required|numeric|min:0');
        $form->number('sell', __('已领取'))->default(0)->rules('required|numeric|min:0');
        $form->number('sort', __('Sort'))->default(0)->rules('required|numeric');
        $form->editor('describe', __('Describe'))->rules('required');
        $form->switch('status', __('Status'))->default(1);
        $form->datetime('start_time', __('Start time'))->rules('required');
        $form->datetime('end_time', '结束时间')->rules('required');

        return $form;
    }
}
